#ifndef RENDERINGLAYER_H
#define RENDERINGLAYER_H


#include <list>
#include <General/cvMemory.h>
#include "cvRenderlist.h"

namespace cvRenderInterface
{
	/**
	 *	Renderinlayer holds the renderinglist and 2D spacehash for the collecting phase.
	 */
	class Renderinglayer
	{
		public:
			Renderinglayer();
			~Renderinglayer();

			/// Destroys all nodes in renderinlayer
			void destroy();

			/// THE MAIN RENDERING LIST
			renderinglist_t renderList;

			/// #3 Z-ordering.
			void triggerZSort( /* DrawlistNode * node*/ );
			/** Draw priority works in two levels:
			 * First level is rough DrawlistNodes ordering
			 * Second level is sorting CollectionNodes internaly.
			 */
			void processZSort();

			/// #4 Rendering list managment
			void processCollections();

			/// Locks layer for extreme fast rendering. Layer contents
			/// become static data, which does not change until
			/// locking is disabled.
			void lockLayer(bool enable);

		protected:
		private:
			bool need_reordering;

			/// #4 processCollections():
			// Searches for nodes that are compatible with each other
			bool search_collection_range( const renderinglist_it_t & begin, renderinglist_it_t & end ) const;
			static bool node_qualifiable( const renderinglist_it_t & ait, const renderinglist_it_t & bit );
			// Searches for collection node from range that contains most amount of Renderables.
			// @returns iterator to colection node or end.
			static renderinglist_it_t  get_collection_from_range( renderinglist_it_t it, const renderinglist_it_t &end);
	};

};


#endif // RENDERINGLAYER_H
